
<!doctype html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.4/favicon.ico">
    <link rel="canonical" href="https://getbootstrap.com/docs/3.4/examples/dashboard/">

    <title>基于Django的气象数据采集和分析</title>

    <!-- Bootstrap core CSS -->
    <link href="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.4/dist/css/bootstrap.min.css" rel="stylesheet">

    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <link href="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.4/assets/css/ie10-viewport-bug-workaround.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.4/examples/dashboard/dashboard.css" rel="stylesheet">

    <!-- Just for debugging purposes. Don't actually copy these 2 lines! -->
    <!--[if lt IE 9]><script src="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.4/assets/js/ie8-responsive-file-warning.js"></script><![endif]-->
    <script src="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.4/assets/js/ie-emulation-modes-warning.js"></script>

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>

  <body>

    <nav class="navbar navbar-inverse navbar-fixed-top">
      <div class="container-fluid">
        <div class="navbar-header">
          <a class="navbar-brand" href="#">基于Django的气象数据采集和分析</a>
        </div>
        <div id="navbar" class="navbar-collapse collapse">
          <ul class="nav navbar-nav navbar-right">
            <li><a href="#">欢迎登陆</a></li>
            <li><a href="login.html">退出</a></li>
          </ul>
        </div>
      </div>
    </nav>

    <div class="container-fluid">
      <div class="row">
        <div class="col-sm-3 col-md-2 sidebar">
          <ul class="nav nav-sidebar">
            <li><a href="crawler.html">数据爬取</a></li>
            <li><a href="user.html">用户管理</a></li>
            <li class="active"><a href="city.html">城市分析</a></li>
            <li><a href="meteorology.html">气象分析</a></li>
            <li><a href="max_temperature.html">最高温度升序排行榜</a></li>
            <li><a href="max_temperature_desc.html">最高温度降序排行榜</a></li>
            <li><a href="min_temperature.html">最低温度升序排行榜</a></li>
            <li><a href="min_temperature_desc.html">最低温度降序排行榜</a></li>
          </ul>
        </div>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
          <h1 class="page-header">城市分析</h1>
          <!-- 搜索框 -->
          <div class="panel-body">
              <form role="form" id="searchForm" class="form-inline">
                  <div class="form-group"> <input type="text"
                          class="form-control" id="name" name="name" placeholder="请输入城市名称">
                  </div>
                  <div class="form-group"> <input type="text"
                          class="form-control" id="year" name="year" placeholder="请输入年份">
                  </div>
                  <div class="form-group">
                      <button type="button" id="searchBtn" class="btn btn-default">开始搜索</button>
                  </div>
                  <div class="form-group">
                      <button type="button" id="radarBtn" class="btn btn-default">雷达图分析</button>
                  </div>
              </form>
          </div>
          <table id="table"></table>
        </div>
      </div>
    </div>
    <!-- 操作模态框 -->
    <div class="modal fade" id="opDataxJobRowModal" tabindex="-1" role="dialog" >
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="exampleModalLabel">城市分析</h4>
          </div>
          <div id="loading" style="height: 400px;">
            <div id="container" style="height: 500%; width: 600%"></div>
          </div>
        </div>
      </div>
    </div>



    <!-- 操作模态框 -->
    <div class="modal fade" id="opDeleteModal" tabindex="-1" role="dialog" >
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="exampleModalLabel">警告</h4>
          </div>
          <div class="modal-body">
                  <strong id="warningText"></strong>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            <button type="button" class="btn btn-danger" id="confirmBtn"></button>
          </div>
        </div>
      </div>
    </div>
    <!-- 操作成功提示模态框 -->
    <div id="opSuccessModal" class="modal fade " tabindex="-1" role="dialog" >
      <div class="modal-dialog modal-sm" role="document">

        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">提示</h4>
            </div>
            <div class="modal-body">
                <p style="color:green">操作成功，删除成功</p>
            </div>
        </div>
      </div>
    </div>
    <!-- 操作失败提示模态框 -->
    <div id="opFailedModal" class="modal fade " tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel">
      <div class="modal-dialog modal-sm" role="document">

        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">提示</h4>
            </div>
            <div class="modal-body">
                <p style="color:red" id="opFaildeMessage">操作失败，该用户不存在</p>
            </div>
        </div>
      </div>
    </div>

    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js" integrity="sha384-nvAa0+6Qg9clwYCGGPpDQLVpLNn0fRaROjHqs13t4Ggj3Ez50XnGQqc/r8MhnRDZ" crossorigin="anonymous"></script>
    <script>window.jQuery || document.write('<script src="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.4/assets/js/vendor/jquery.min.js"><\/script>')</script>
    <script src="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.4/dist/js/bootstrap.min.js"></script>
    <!-- Just to make our placeholder images work. Don't actually copy the next line! -->
    <script src="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.4/assets/js/vendor/holder.min.js"></script>
    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <script src="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.4/assets/js/ie10-viewport-bug-workaround.js"></script>
    <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/echarts@5/dist/echarts.min.js"></script>
    <script src="assets/js/table/bootstrap-table.js"></script>
    <script src="assets/js/table/bootstrap-table-zh-CN.js"></script>
    <script type="text/javascript">
      var myChart='';
      var message='';
      var dom = document.getElementById("container");
      var data = [];
      // 通用初始化模板
      function initEcharts(data){
        myChart = echarts.init(dom);
        var app = {};
        var option;
        option = {
          tooltip: {
              trigger: 'item'
          },
          radar: {
              // shape: 'circle',
              indicator: [
                  { name: '最高温度均值(℃)', max: 50},
                  { name: '最低温度均值(℃)', max: 50},
                  { name: '温度均值(℃)', max: 50}
              ]
          },
          series: [{
              name: '预算 vs 开销（Budget vs spending）',
              type: 'radar',
              data: data
          }]
        };

        if (option && typeof option === 'object') {
            myChart.setOption(option,true);
        }
      }
      // 初始化表格数据
      $(function(){
        init()
      });
      function init(){
        $('#table').bootstrapTable("showLoading");
        $.ajax({ 
          url: "http://127.0.0.1:8082/api/crawler/get_city_by_name_or_year/",
          method: "get",
          dataType: "json",
          success: function(rs){
            message = rs.results
            $('#table').bootstrapTable('load', message);
            $('#table').bootstrapTable('refresh');
            $('#table').bootstrapTable("hideLoading");
          },
        })
      };
      // 初始化表格
      $('#table').bootstrapTable({
        cache:false,
        clickToSelect:true,
        pageSize: 15,
        pageNumber: 1,
        pagination: true,
        columns: [{
          field: 'id',
            title: '编号'
          }, {
            field: 'name',
            title: '城市'
          }, {
            field: 'year',
            title: '年份'
          }, {
            field: 'avg_top_temperature',
            title: '最高温度均值(℃)'
          }, {
            field: 'avg_low_temperature',
            title: '最低温度均值(℃)'
          }, {
            field: 'avg_temperature',
            title: '温度均值(℃)'
          }, {
            field: 'operate',
            title: '操作',
            formatter:function(value,row,index){
                    return '<a  id="runDataxJob" class="btn btn-xs btn-danger"><i class="fa fa-delete"></i> 删除</a>';
                },
                events: {
            'click #runDataxJob': function (e, value, row, index) {
              //动态控制模态框的显示内容，为模态框的按钮动态的绑定事件，包括删除事件和执行事件，
              //这样模态框就可以复用
              $("#warningText").text("是否删除城市为： " +row.name);
              $("#confirmBtn").text("确认");
              $("#confirmBtn").on("click",function(){
                delete_city(row)
              });
              $("#opDeleteModal").modal();
            }
          }
        }]
      });
      // 搜索
      $("#searchBtn").click(function() {
        var name = $("#name").val();
        var year = $("#year").val();
        $.ajax({
          type: "get",
          url: "http://127.0.0.1:8082/api/crawler/get_city_by_name_or_year/",
          cache: false,
          data: {
            name : name,
            year : year
          },
          success : function(res) {
            message = '';
            message = res.results
            $("#table").bootstrapTable('load', message);//主要是要这种写法
            $('#table').bootstrapTable('refresh');
          }
        });
      });
      // 雷达图分析
      $("#radarBtn").click(function() {
        $("#opDataxJobRowModal").modal('toggle');
        for (var i=0; i<message.length;i++){
          var value = [];
          var series = {};
          value.push(parseFloat(message[i].avg_top_temperature))
          value.push(parseFloat(message[i].avg_low_temperature))
          value.push(parseFloat(message[i].avg_temperature))
          series['value'] = value
          series['name'] = message[i].year+"年"+message[i].name
          data.push(series);
        }
        initEcharts(data);
      });


      // 删除用户
      function delete_city(row){
        $.ajax({
           type: "delete",
           url: "http://127.0.0.1:8082/api/crawler/delete_city",
           cache: false,
           data: {id : row.id},
           success : function(res) {
            $("#opDeleteModal").modal('toggle');
              if(res.status == "ok"){
                $("#opSuccessModal").modal();
                init()
              }else{
                $("#opFailedModal").modal();
              }
            }
        });
      }

      // 关闭模态框后状态
      $('#opDataxJobRowModal').on('hidden.bs.modal', function () {
          myChart.clear();
          myChart = '';
          data = [];
      })
    </script>
  </body>
</html>
